import { Base } from "./base";

export class Survey extends Base {
  /**
   *
   * @param name
   * @param cate_id
   * @param pageNum
   * @param pageSize
   * @returns {Promise<void>}
   */
  // eslint-disable-next-line camelcase
  async list({ cate_id, name, pageNum = 1, pageSize = 10 }) {
    const { data = {} } = await this.$http("post", "/api/survey", {
      cate_id,
      name,
      pageNum,
      pageSize
    });
    return data;
  }

  /**
   *
   * @param id
   * @returns {Promise<boolean>}
   */
  async del(id) {
    const { errno } = await this.$http("post", "/api/survey/del", { id });
    return errno === 0;
  }

  /**
   *
   * @param form
   * @returns {Promise<void>}
   */
  async add(form) {
    const { errno, data } = await this.$http("post", "/api/survey/add", {
      ...form
    });
    return errno === 0 ? data : false;
  }

  /**
   *
   * @param id
   * @param form
   * @returns {Promise<void>}
   */
  // eslint-disable-next-line camelcase
  async update({ id, ...form }) {
    const { errno } = await this.$http("post", "/api/survey/update", {
      id,
      ...form
    });
    return errno === 0;
  }

  /**
   *
   * @param id
   * @returns {Promise<void>}
   */
  async detail(id) {
    const { data } = await this.$http("post", "/api/survey/detail", {
      id
    });
    return data;
  }

  /**
   *
   * @param survey_id
   * @returns {Promise<void>}
   */
  async questionList({ survey_id, pageSize, pageNum, order }) {
    const { data } = await this.$http("post", "/api/survey/question", {
      survey_id,
      pageSize,
      pageNum,
      order
    });
    return data;
  }

  async questionDel(id) {
    const { errno } = await this.$http("post", "/api/survey/question/del", {
      id
    });
    return errno === 0;
  }

  async questionAdd(form) {
    const { data } = await this.$http("post", "/api/survey/question/add", {
      ...form
    });
    return data;
  }

  async questionUpdate(form) {
    const { data } = await this.$http("post", "/api/survey/question/update", {
      ...form
    });
    return data;
  }

  async questionDetail(id) {
    const { data } = await this.$http("post", "/api/survey/question/detail", {
      id
    });
    return data;
  }

  async optionList({ question_id, pageSize, pageNum, order }) {
    const { data } = await this.$http("post", "/api/survey/option", {
      question_id,
      pageSize,
      pageNum,
      order
    });
    return data;
  }

  async optionDel(id) {
    const { errno } = await this.$http("post", "/api/survey/option/del", {
      id
    });
    return errno === 0;
  }

  async optionAdd(form) {
    const { data } = await this.$http("post", "/api/survey/option/add", {
      ...form
    });
    return data;
  }

  async optionUpdate(form) {
    const { data } = await this.$http("post", "/api/survey/option/update", {
      ...form
    });
    return data;
  }

  async optionDetail(id) {
    const { data } = await this.$http("post", "/api/survey/option/detail", {
      id
    });
    return data;
  }
}
